﻿using System;
using System.ComponentModel;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using _4chanManager.Logic;
using _4chanManager.Properties;

namespace _4chanManager.Panels
{
    /// <summary>
    /// Interaction logic for ThreadWatcherPanel.xaml
    /// </summary>
    public partial class ThreadWatcherPanel : UserControl
    {
        private BackgroundWorker _bgw;
        private FileDownloader _downloader;
        private int SliderValue = 0;

        public ThreadWatcherPanel()
        {
            InitializeComponent();
            Init();
        }
        private void Init()
        {
            _bgw = new BackgroundWorker { WorkerReportsProgress = true };
            _bgw.DoWork += bgw_DoWork;

            txtExtensions.Text = (string)Settings.Default["strExtensions"];
            string destinationFolder = (string)Settings.Default["strDestination"];
            if (!string.IsNullOrEmpty(destinationFolder))
                pathDestination.Path = destinationFolder;
        }

        private void bgw_DoWork(object sender, DoWorkEventArgs e)
        {
            _downloader.Start();
        }
        
        void _downloader_StatusChanged(bool? value)
        {
             if (!value.HasValue)
            {
                txtStatus.Dispatcher.Invoke(() => 
                txtStatus.Text = "unknown");
                txtStatus.Dispatcher.Invoke(() => 
                txtStatus.Foreground = new SolidColorBrush(Colors.Black));
                return;
            }
            if (value.Value)
            {
            
                txtStatus.Dispatcher.Invoke(() => 
                txtStatus.Text = "ALIVE");
                txtStatus.Dispatcher.Invoke(() => 
                txtStatus.Foreground = new SolidColorBrush(Colors.Green));
                return;
            }

            txtStatus.Dispatcher.Invoke(() => 
            txtStatus.Text = "DEAD/404");
            txtStatus.Dispatcher.Invoke(() => 
            txtStatus.Foreground = new SolidColorBrush(Colors.DarkRed));
        }

        private void EnableDissableControls(bool value)
        {
            btnStartStop.IsEnabled = value;
            sliderRefresh.IsEnabled = value;
            pathWebAdress.IsReadOnly = value;
            pathDestination.IsReadOnly = value;
        }

        private void btnStartStop_Click(object sender, RoutedEventArgs e)
        {
            EnableDissableControls(false);
            _downloader = new FileDownloader(pathWebAdress.Path, txtExtensions.Text, pathDestination.Path);
            _downloader.StatusChanged += _downloader_StatusChanged;
            _downloader.RefreshTreadEvery = SliderValue;
            _bgw.RunWorkerAsync();
        }


        private void sliderRefresh_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            lblMinutes.Content = e.NewValue + " min";
            SliderValue = Int32.Parse(e.NewValue.ToString(CultureInfo.InvariantCulture));

        }

        private void btnSaveExtensions_Click(object sender, RoutedEventArgs e)
        {
            Settings.Default["strExtensions"] = txtExtensions.Text;
            Settings.Default.Save();
        }

        private void btnSaveDestination_Click(object sender, RoutedEventArgs e)
        {
            Settings.Default["strDestination"] = pathDestination.Path;
            Settings.Default.Save();
        }
    }
}
